package easybooking.db;

import easybooking.data.*;
import java.rmi.RemoteException;
import java.sql.*;
import java.util.Vector;

// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.09DFE639-C9BF-3647-E4DA-E652E9254625]
// </editor-fold> 
public class AirlineMapper {

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.52C8E148-8AFB-BC5B-7893-D4CC737EFC41]
    // </editor-fold> 
    private static AirlineMapper airlineMapper = null;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.D144130D-F13B-5575-F84A-E9D044D7D953]
    // </editor-fold> 
    public static AirlineMapper airlineMapper() {
        if (airlineMapper == null) {
            airlineMapper = new AirlineMapper();
        }
        return airlineMapper;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.EA5CFDD2-DAB9-AA67-93AE-E3995D02D678]
    // </editor-fold> 
    public Airline findByKey (int id) {
        Connection con = DBConnection.connection();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                "SELECT airlineId, name, initials, isActive FROM Airline " +
                "WHERE airlineId=" + id +
                " ORDER BY airlineId ASC");
            if (rs.next()) {
                try {
                    Airline a = new AirlineImpl();
                    a.setAirlineId(rs.getInt("airlineId"));
                    a.setName(rs.getString("name"));
                    a.setInitials(rs.getString("initials"));
                    a.setIsActive(rs.getBoolean("isActive"));
                    return a;
                } catch (Exception ex) {
                    ex.printStackTrace();
                    return null;
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
            return null;
        }
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.E8C03A59-76FA-3517-2619-5B498BDD45F9]
    // </editor-fold> 
     public Vector<Airline> findAll() {
        Connection con = DBConnection.connection();
        Vector<Airline> result = new Vector<Airline>();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                "SELECT airlineId, name, initials, isActive FROM Airline " +
                "ORDER BY airlineId ASC");
            while (rs.next()) {
                try {
                    Airline a = new AirlineImpl();
                    a.setAirlineId(rs.getInt("airlineId"));
                    a.setName(rs.getString("name"));
                    a.setInitials(rs.getString("initials"));
                    a.setIsActive(rs.getBoolean("isActive"));
                    result.addElement(a);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    break;
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        return result;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.2535AF48-8A30-8194-AD04-8850C8C0D5AC]
    // </editor-fold> 
    public Airline insert(Airline a) {
        Connection con = DBConnection.connection();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                "SELECT MAX(airlineId) AS maxAirlineId " +
                "FROM Airline");
            if (rs.next()) {
                try {
                    a.setAirlineId(rs.getInt("maxAirlineId") + 1);
                    stmt = con.createStatement();
                    stmt.executeUpdate("INSERT INTO Airline (airlineId, name, initials, isActive) " +
                            "VALUES (" +
                            a.getAirlineId() + ",'" +
                            a.getName() + "','" +
                            a.getInitials() + "','" +
                            a.getIsActive() + "')");
                }
                catch (RemoteException ex) {
                    ex.printStackTrace();
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        return a;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.AE09B511-ABA6-2345-D842-A1A18F5FE354]
    // </editor-fold> 
    public Airline update(Airline a) {
        Connection con = DBConnection.connection();
        try {
            Statement stmt = con.createStatement();
            stmt.executeUpdate("UPDATE Airline " +
            "SET name=\"" + a.getName() + "\", " +
            "initials=\"" + a.getInitials() + "\" " +
            "isActive=\"" + a.getIsActive() + "\" " +
            "WHERE airlineId=" + a.getAirlineId());
        } catch (SQLException e2) {
            e2.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return a;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.B4EC0E29-6733-A115-523E-8FA68FBF3D16]
    // </editor-fold> 
    public Vector<Airline> findByName(String name) {
        Connection con = DBConnection.connection();
        Vector<Airline> result = new Vector<Airline>();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                "SELECT airlineId, name, initials, isActive FROM Airline " +
                "WHERE name LIKE " + name +
                "ORDER BY name ASC");
            while (rs.next()) {
                try {
                    Airline a = new AirlineImpl();
                    a.setAirlineId(rs.getInt("airportId"));
                    a.setName(rs.getString("name"));
                    a.setInitials(rs.getString("initials"));
                    a.setIsActive(rs.getBoolean("isActive"));
                    result.addElement(a);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    break;
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        return result;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.BE627711-B62F-D2EE-CCE7-B2905B94FCE5]
    // </editor-fold> 
    public Vector<Airline> findByInitials(String initials) {
        Connection con = DBConnection.connection();
        Vector<Airline> result = new Vector<Airline>();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                "SELECT airlineId, name, initials, isActive FROM Airline " +
                "WHERE initials LIKE " + initials +
                "ORDER BY name ASC");
            while (rs.next()) {
                try {
                    Airline a = new AirlineImpl();
                    a.setAirlineId(rs.getInt("airportId"));
                    a.setName(rs.getString("name"));
                    a.setInitials(rs.getString("initials"));
                    a.setIsActive(rs.getBoolean("isActive"));
                    result.addElement(a);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    break;
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        return result;
    }
}

